//
//  YLOrderViewController.m
//  YLFinanceService
//
//  Created by she1323 on 2017/9/7.
//  Copyright © 2017年 she1323. All rights reserved.
//

#import "YLOrderViewController.h"
#import <Masonry.h>
#import "YLNewDeliveryAddressTableViewCell.h"
#import "YLGlobeHeader.h"
#import "YLOrderGoodsInfoCell.h"
#import "YLOrderGoodsNumberTableViewCell.h"
#import "YLOrderIntroduceCell.h"
#import "YLMallGoodExchangeBottomTab.h"
#import "YLOrderViewModel.h"
#import "YLExchangeDetailController.h"
#import "YLEditAdressViewController.h"
#import "YLChoiceAdressTableViewCell.h"
#import "YLAdressTableViewCell.h"
#import "YLChoiceAdressViewController.h"
#import "YLMyAdressModel.h"
@interface YLOrderViewController ()<UITableViewDelegate, UITableViewDataSource, YLEditAddressViewControllerDelegate>

@property (nonatomic, strong) YLOrderViewModel *viewModel;
@end

@implementation YLOrderViewController

- (YLOrderViewModel *)viewModel
{
    if (!_viewModel) {
        _viewModel = [[YLOrderViewModel alloc] init];
    }
    return _viewModel;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"订单填写";
    
    self.viewModel.infoModel = _model;
    
    [self.tableView reloadData];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self initTableView];
    
    WHNavBackButton *backButton = [WHNavBackButton navBackButton];
    [backButton setImage:[UIImage imageNamed:@"fanhuijianblack"] forState:UIControlStateNormal];
    [backButton addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:backButton];
    
}



- (void)back
{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)initTableView
{
    __weak typeof(self) weakSelf = self;
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLOrderGoodsInfoCell class]) bundle:nil] forCellReuseIdentifier:[YLOrderGoodsInfoCell cellID]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLOrderGoodsNumberTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLOrderGoodsNumberTableViewCell cellID]];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLOrderIntroduceCell class]) bundle:nil] forCellReuseIdentifier:[YLOrderIntroduceCell cellID]];
    
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.right.left.mas_equalTo(weakSelf.view);
        
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom).mas_offset(-64);
        } else {
                make.bottom.mas_equalTo(weakSelf.view).mas_offset(-64);
        }
        
    }];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLAdressTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLAdressTableViewCell cellID]];
    
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YLChoiceAdressTableViewCell class]) bundle:nil] forCellReuseIdentifier:[YLChoiceAdressTableViewCell cellID]];
    
    
    [self initBottom];
}


- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.viewModel.groups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (section == 0) {
        id obj = self.viewModel.groups[section];
        if ([obj isKindOfClass:[YLHaveNoAdressModel class]]) {
            // 如果没有地址，2行
            return 1;
        } else {
            return 2;
        }
    }

    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
        id obj = self.viewModel.groups[0];
        if ([obj isKindOfClass:[YLHaveNoAdressModel class]]) {
            YLNewDeliveryAddressTableViewCell *cell = [YLNewDeliveryAddressTableViewCell cellWithTableView:tableView];
            return cell;
        } else {
            if (indexPath.row == 0) {
                YLChoiceAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLChoiceAdressTableViewCell cellID]];
                return cell;
            } else {
                YLAdressTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLAdressTableViewCell cellID]];
                cell.hiddenIcon = YES;
                cell.model = obj;
                cell.contentView.backgroundColor = YLColor(94, 96, 107);
                return cell;
            }
        }
        
    } else if (indexPath.section == 1) {
        YLOrderGoodsInfoCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLOrderGoodsInfoCell cellID]];
        cell.model = self.viewModel.groups[indexPath.section];
        return cell;
    } else if (indexPath.section == 2) {
        YLOrderGoodsNumberTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLOrderGoodsNumberTableViewCell cellID]];
        cell.model = self.viewModel.groups[indexPath.section];
        return cell;
    } else if (indexPath.section == 3) {
         YLOrderIntroduceCell *cell = [tableView dequeueReusableCellWithIdentifier:[YLOrderIntroduceCell cellID]];
        return cell;
    }
    return [[UITableViewCell alloc] init];
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 0.001;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return [UIView new];
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 10;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [self.tableView reloadSections:[NSIndexSet indexSetWithIndex:0] withRowAnimation:UITableViewRowAnimationNone];
    [self.tableView reloadData];
    [self.navigationController.navigationBar setBarStyle:UIBarStyleDefault];
    [self.navigationController.navigationBar setBackgroundImage:nil forBarMetrics:UIBarMetricsDefault];
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName:YLColor(50, 50, 50)};
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleDefault;
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [self.navigationController.navigationBar setBackgroundImage:[UIImage imageNamed:@"navBackground"] forBarMetrics:UIBarMetricsDefault];
    // 修改标题颜色
    self.navigationController.navigationBar.titleTextAttributes = @{NSForegroundColorAttributeName: [UIColor whiteColor]};
    self.navigationController.navigationBar.barStyle = UIStatusBarStyleLightContent;
}


- (void)initBottom
{
    __weak typeof(self) weakSelf = self;
    YLMallGoodExchangeBottomTab *tab = [[YLMallGoodExchangeBottomTab alloc] init];
    tab.totalPrice = YES;
    tab.neetPoint = _model.totalNeetPoint;
    
    [self.view addSubview:tab];
    
    [tab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(weakSelf.view);
        make.top.mas_equalTo(weakSelf.tableView.mas_bottom);
        
        if (@available(iOS 11.0, *)) {
            make.bottom.mas_equalTo(weakSelf.view.mas_safeAreaLayoutGuideBottom);
        } else {
            make.bottom.mas_equalTo(weakSelf.view);
        }
        
    }];
    
    [tab.exchangeSubject subscribeNext:^(id  _Nullable x) {
        YLExchangeDetailController *detail = [[YLExchangeDetailController alloc] init];
        
        id model =  weakSelf.viewModel.groups.firstObject;
        if ([model isKindOfClass:[YLMyAdressModel class]]) {
            weakSelf.model.addressModel = model;
        }
        detail.model = weakSelf.model;
        
        [weakSelf addChildViewController:detail];
        detail.view.frame = [UIScreen mainScreen].bounds;
        
        if (@available(iOS 11.0, *)) {
            detail.view.frame = CGRectMake(0, 0, YLScreenW, YLScreenH - self.view.safeAreaInsets.bottom);
        } else {
            detail.view.frame = [UIScreen mainScreen].bounds;
           // make.bottom.mas_equalTo(weakSelf.view).mas_offset(-44);
        }
        detail.view.backgroundColor = YLColorA(0, 0, 0, 0.4);
        [weakSelf.view addSubview:detail.view];
    }];
    
}



- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (indexPath.section == 0) {
        id obj = self.viewModel.groups[indexPath.section];
        if ([obj isKindOfClass:[YLHaveNoAdressModel class]]) {
            // 添加新地址
            YLEditAdressViewController *edit = [[YLEditAdressViewController alloc] init];
            edit.title = @"新增收货地址";
            edit.delegate = self;
            [self.navigationController pushViewController:edit animated:YES];
        } else {
            if (indexPath.row == 0) {
                YLChoiceAdressViewController *adress = [[YLChoiceAdressViewController alloc] init];
                adress.model = obj;
                [self.navigationController pushViewController:adress animated:YES];
            }
        }
    }
}



- (void)edidAddressViewController:(YLEditAdressViewController *)controller addAddress:(YLMyAdressModel *)model
{
    
    [self.viewModel.groups replaceObjectAtIndex:0 withObject:model];
    [self.tableView reloadData];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
